package com.example.springbootmpdemo.controller;


import com.example.springbootmpdemo.entity.Member;
import com.example.springbootmpdemo.entity.Team;
import com.example.springbootmpdemo.service.IMemberService;
import com.example.springbootmpdemo.service.ITeamService;
import com.example.springbootmpdemo.service.impl.MemberServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author metasequoia
 * @since 2021-12-19
 *
 * resources→static下每个html文件，提交表单的url地址，都在这里进行逻辑上的处理
 */
@Controller
@RequestMapping("/member")
public class MemberController {

    //为什么非要“注入”这个对象？
    @Autowired
    private IMemberService iMemberService;

    @Autowired
    private ITeamService iTeamService;

    //注意返回值是ModelAndView！！返回这个类型，进入相应的html页
    //说了要四个值，就得都给它！不然就globelError
    @PostMapping("/add")
    public ModelAndView addMember(@RequestParam String password,
                                  @RequestParam int teamid,
                                  @RequestParam String name,
                                  @RequestParam String sex,
                                  @RequestParam String tel){
        Member member = new Member();
        member.setName(name);
        member.setSex(sex);
        member.setTeamid(teamid);
        member.setPassword(password);
        member.setTel(tel);

        //getById或save如果成功则正常，但一错就进/error,我希望它正常点返回个false就行了
        //直接在地址栏输memberSucess.html,它也照例是去globeError
        /*  现在memberid自增长了，所以不需要判断是否重复
        if(iMemberService.getById(memberid)!=null){
            ModelAndView mv = new ModelAndView("memberError");
            //直接一个字符串，也可以“取名”
            mv.addObject("error","该成员ID已被占用！");
            return mv;
        }
         */

        //我能不能靠这个判断，就是让iTeamService去getById，如果不存在，则报相应的错误
        if(iTeamService.getById(teamid)==null){
            ModelAndView mv = new ModelAndView("memberError");
            //直接一个字符串，也可以“取名”
            mv.addObject("error","不存在队伍ID为"+teamid+"的队伍！");
            return mv;
        }

        if(iMemberService.save(member)){
            //这里的memberSuccess是个html页，需要自己在templates下新建
            ModelAndView mv = new ModelAndView("memberSuccess");
            //这里给mv对象（将会到memberSuccess页）添加名为“member”的member类型对象
            mv.addObject("member",member);
            return mv;
        }else{
            ModelAndView mv = new ModelAndView("memberError");
            mv.addObject("error","一般来说你不会见到这个错误的，要错就直接globelError了");
            return mv;
        }

    }

    @PostMapping("/login")
    public ModelAndView login(@RequestParam int memberid,
                              @RequestParam String password){
        //根据memberid获取member对象
        Member member = iMemberService.getById(memberid);
        //若为空则显示不存在
        if(member==null){
            ModelAndView mv = new ModelAndView("loginError");
            //直接一个字符串，也可以“取名”
            mv.addObject("error","该成员ID不存在！");
            return mv;
        }

        if(!member.getPassword().equals(password)){
            ModelAndView mv = new ModelAndView("loginError");
            mv.addObject("error","成员密码错误！");
            return mv;
        }

        Team team = iTeamService.getById(member.getTeamid());

        ModelAndView mv = new ModelAndView("LoginSuccess");
        //这里给mv对象（将会到memberSuccess页）添加名为“member”的member类型对象
        mv.addObject("member",member);
        mv.addObject("team",team);
        return mv;
    }

    //直接让用户加入队伍
    @PostMapping("/join")
    public ModelAndView join(@RequestParam int memberid,
                             @RequestParam int teamid){

        //因为已经经过登录了，所以这里不需要验证密码！
        Member member = iMemberService.getById(memberid);

        //teamid要验证一下
        Team team = iTeamService.getById(teamid);
        if(team==null){
            ModelAndView mv = new ModelAndView("teamError");
            mv.addObject("error","该队伍ID不存在！要不您受累，给创建一个？");
            mv.addObject("member",member);
            return mv;
        }

        member.setTeamid(teamid);
        //刚刚忘记更新数据库了，传到joinSuccess页的只是一个member对象囧
        iMemberService.updateById(member);

        ModelAndView mv = new ModelAndView("joinSuccess");
        mv.addObject("member",member);
        mv.addObject("team",team);
        return mv;
    }


    @PostMapping("/set")
    public ModelAndView set(@RequestParam int memberid,
                            @RequestParam int teamid,
                            @RequestParam String password,
                            @RequestParam String name,
                            @RequestParam String sex,
                            @RequestParam String tel){
        Member member = new Member();
        member.setMemberid(memberid);
        member.setTeamid(teamid);
        member.setPassword(password);
        member.setName(name);
        member.setSex(sex);
        member.setTel(tel);

        iMemberService.updateById(member);

        ModelAndView mv = new ModelAndView("setMemberSuccess");
        mv.addObject("member",member);
        return mv;
    }

    @PostMapping("/who")
    public ModelAndView who(@RequestParam int memberid,
                            @RequestParam int whoid){
        Member member = iMemberService.getById(memberid);
        Member who = iMemberService.getById(whoid);

        if(who==null){
            ModelAndView mv = new ModelAndView("loginError");
            //直接一个字符串，也可以“取名”
            mv.addObject("error","该成员ID不存在！");
            return mv;
        }

        String name = who.getName();
        ModelAndView mv = new ModelAndView("whoSuccess");
        mv.addObject("name",name);
        mv.addObject("member",member);
        return mv;

    }



}
